package com.airsystem.pos.bengkel.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;

import com.airsystem.pos.bengkel.controller.MenuController;
import com.airsystem.pos.bengkel.controller.SparepartOrderController;
import com.airsystem.pos.bengkel.util.PropertyReader;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class SparepartOrder extends SparepartOrderController {

	public SparepartOrder(MenuController menuController) {
		super();

		this.menuController = menuController;
		initComponents();
		setup();
	}

	private void initComponents() {
		setTitle("Pesan Sparepart");
		setFrameIcon(new ImageIcon(getClass().getResource("/images/spareparty.png")));
		setIconifiable(true);
		setMaximizable(true);
		setClosable(true);
		scrollPane.setViewportView(basePanel);

		baseLayout.columnWidths = new int[] { 0 };
		baseLayout.rowHeights = new int[] { 0 };
		baseLayout.columnWeights = new double[] { 1.0 };
		baseLayout.rowWeights = new double[] { 1.0 };
		getContentPane().setLayout(baseLayout);

		panelLayout.columnWidths = new int[] { 150, 300, 0 };
		panelLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		panelLayout.columnWeights = new double[] { 0.0, 1.0, 1.0 };
		panelLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0 };
		basePanel.setLayout(panelLayout);

		tanggalLabel.setFont(new Font("Verdana", 0, 12));
		tanggalLabel.setText("Tanggal");

		sparepartLabel.setFont(new Font("Verdana", 0, 12));
		sparepartLabel.setText("Sparepart");

		kendaraanLabel.setFont(new Font("Verdana", 0, 12));
		kendaraanLabel.setText("Kendaraan");

		jenisLabel.setFont(new Font("Verdana", 0, 12));
		jenisLabel.setText("Jenis Sparepart");

		hargaLabel.setFont(new Font("Verdana", 0, 12));
		hargaLabel.setText("Harga");

		orderLabel.setFont(new Font("Verdana", 0, 12));
		orderLabel.setText("Jumlah Pesan");

		distributorLabel.setFont(new Font("Verdana", 0, 12));
		distributorLabel.setText("Distributor");

		pembayaranLabel.setFont(new Font("Verdana", 0, 12));
		pembayaranLabel.setText("Metode Pembayaran");

		tanggalChooser.setDateFormatString("d MMMM yyyy");
		((JTextField) tanggalChooser.getDateEditor().getUiComponent()).setEditable(false);

		sparepartField.setFont(new Font("Verdana", 0, 12));
		sparepartField.setColumns(15);
		sparepartField.setEditable(false);

		kendaraanField.setFont(new Font("Verdana", 0, 12));
		kendaraanField.setColumns(15);
		kendaraanField.setEditable(false);

		jenisField.setFont(new Font("Verdana", 0, 12));
		jenisField.setColumns(15);
		jenisField.setEditable(false);

		hargaField.setFont(new Font("Verdana", 0, 12));
		hargaField.setColumns(15);
		hargaField.setDocument(new TextUtil(17, TextUtil.NUMBER));

		orderField.setFont(new Font("Verdana", 0, 12));
		orderField.setColumns(15);
		orderField.setDocument(new TextUtil(3, TextUtil.NUMBER));

		distributorField.setFont(new Font("Verdana", 0, 12));
		distributorField.setColumns(15);
		distributorField.setDocument(new TextUtil(30, TextUtil.CAPITAL));

		pembayaranCombo.setFont(new Font("Verdana", 0, 12));
		pembayaranCombo.setModel(new DefaultComboBoxModel(COMBOS));

		idField.setFont(new Font("Verdana", 0, 12));
		idField.setColumns(5);
		idField.setVisible(false);

		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));

		searchButton.setIcon(new ImageIcon(getClass().getResource("/images/search.png")));
		searchButton.setFont(new Font("Verdana", 1, 12));
		searchButton.setText("CARI");

		sparepartButton.setIcon(new ImageIcon(getClass().getResource("/images/select.png")));
		sparepartButton.setFont(new Font("Verdana", 1, 12));
		sparepartButton.setText("PILIH");

		addButton.setIcon(new ImageIcon(getClass().getResource("/images/add.png")));
		addButton.setFont(new Font("Verdana", 1, 12));
		addButton.setText("TAMBAH");
		buttonPanel.add(addButton);

		submitButton.setIcon(new ImageIcon(getClass().getResource("/images/submit.png")));
		submitButton.setFont(new Font("Verdana", 1, 12));
		submitButton.setText("KIRIM");
		buttonPanel.add(submitButton);

		updateButton.setIcon(new ImageIcon(getClass().getResource("/images/update.png")));
		updateButton.setFont(new Font("Verdana", 1, 12));
		updateButton.setText("UBAH");
		buttonPanel.add(updateButton);

		deleteButton.setIcon(new ImageIcon(getClass().getResource("/images/delete.png")));
		deleteButton.setFont(new Font("Verdana", 1, 12));
		deleteButton.setText("HAPUS");
		buttonPanel.add(deleteButton);

		cancelButton.setIcon(new ImageIcon(getClass().getResource("/images/cancel.png")));
		cancelButton.setFont(new Font("Verdana", 1, 12));
		cancelButton.setText("BATAL");
		buttonPanel.add(cancelButton);

		initSpareparts();

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});

		sparepartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				sparepartButtonActionPerformed(evt);
			}
		});

		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				submitButtonActionPerformed(evt);
			}
		});

		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});

		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		basePanel.add(tanggalLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 70, 20, 30), 0, 0));
		basePanel.add(sparepartLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(kendaraanLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(jenisLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(hargaLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(orderLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(distributorLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(pembayaranLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 70, 20, 30), 0, 0));
		basePanel.add(tanggalChooser, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 20, 20, 30), 0, 0));
		basePanel.add(sparepartField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(kendaraanField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(jenisField, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(hargaField, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(orderField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(distributorField, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(pembayaranCombo, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 20, 30), 0, 0));
		basePanel.add(searchButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 20, 20, 0), 0, 0));
		basePanel.add(sparepartButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 20, 0), 0, 0));
		basePanel.add(buttonPanel, new GridBagConstraints(0, 9, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		basePanel.add(idField, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 20, 0), 0, 0));

		getContentPane().add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 800) / 2, (screenSize.height - 700) / 2, 800, 600);
	}

	private void initSpareparts() {
		tablesparepartsScroll.setViewportView(tableSpareparts);
		tableSpareparts.setFont(new Font("Verdana", 0, 12));
		tableSpareparts.setModel(tableModel);

		tableSpareparts.getTableHeader().setReorderingAllowed(false);
		tableSpareparts.getColumnModel().getColumn(0).setPreferredWidth(200);
		tableSpareparts.getColumnModel().getColumn(1).setPreferredWidth(200);
		tableSpareparts.getColumnModel().getColumn(2).setPreferredWidth(150);
		tableSpareparts.getColumnModel().getColumn(3).setPreferredWidth(200);
		tableSpareparts.getColumnModel().getColumn(4).setPreferredWidth(150);
		tableSpareparts.getColumnModel().getColumn(5).setPreferredWidth(250);
		tableSpareparts.getColumnModel().getColumn(6).setPreferredWidth(200);
		tableSpareparts.getColumnModel().getColumn(7).setPreferredWidth(200);

		/* Hide Column */
		tableSpareparts.getColumnModel().getColumn(8).setMaxWidth(0);
		tableSpareparts.getColumnModel().getColumn(8).setMinWidth(0);
		tableSpareparts.getColumnModel().getColumn(8).setPreferredWidth(0);

		tableSpareparts.setPreferredScrollableViewportSize(new Dimension(150, 150));
		tableSpareparts.setFillsViewportHeight(true);

		tableSpareparts.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				tableSparepartsMouseClicked(evt);
			}
		});

		fieldPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));

		totalLabel.setFont(new Font("Verdana", 0, 12));
		totalLabel.setText("Grand Total");
		fieldPanel.add(totalLabel);

		totalField.setFont(new Font("Verdana", 0, 12));
		totalField.setColumns(15);
		totalField.setEditable(false);
		fieldPanel.add(totalField);

		basePanel.add(tablesparepartsScroll, new GridBagConstraints(0, 10, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 1, 1, 1), 0, 0));
		basePanel.add(fieldPanel, new GridBagConstraints(0, 11, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 30, 0, 0), 0, 0));
	}

	private void searchButtonActionPerformed(ActionEvent evt) {
		search();
	}

	private void sparepartButtonActionPerformed(ActionEvent evt) {
		get();
	}

	private void tableSparepartsMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2 && tableSpareparts.getRowCount() != 0) {
			row = tableSpareparts.getSelectedRow();
			isGridClicked = true;

			init();
		}
	}

	private void addButtonActionPerformed(ActionEvent evt) {
		add();
	}

	private void submitButtonActionPerformed(ActionEvent evt) {
		submit();
	}

	private void updateButtonActionPerformed(ActionEvent evt) {
		edit();
	}

	private void deleteButtonActionPerformed(ActionEvent evt) {
		if (!isGridClicked) {
			if (!PropertyReader.instance().isCanDeleteTransactionHistory()) {
				return;
			}

			delete();
		} else {
			remove();
		}

		isGridClicked = false;
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		cancel();
	}
}